/*
   CC.NET SideBar Gadget for Windows Vista 
   (C) Simone Chiaretta, 2007
   http://codeclimber.net.nz

   This project is licensed under the new BSD license.
   See the License.txt file for more information.
*/
var DisplayModeEnum = { All:0, OnlyFailing:1}

var Settings =
{
   RefreshInterval: 30,
   DisplayMode: DisplayModeEnum.All,
   AutoScrollEnabled: false,
   AutoScrollInterval: 5,
   ProjectList: new Array(),
   GetLastUsedServer: function()
   {
      var server = System.Gadget.Settings.read("LastUsedServer");
      if(server == undefined)
         server ="";
      return server;
   },
   SetLastUsedServer: function(value)
   {
      System.Gadget.Settings.write("LastUsedServer", value);
   },
   Save: function()
   {
      System.Gadget.Settings.write("RefreshInterval", this.RefreshInterval);
      System.Gadget.Settings.write("DisplayMode", this.DisplayMode);
      System.Gadget.Settings.write("AutoScrollEnabled", this.AutoScrollEnabled);
      System.Gadget.Settings.write("AutoScrollInterval", this.AutoScrollInterval);
      System.Gadget.Settings.write("ProjectList",this.SerializeProjectList());
   },
   Read: function()
   {
      this.RefreshInterval = parseInt(System.Gadget.Settings.read("RefreshInterval"));
      if(isNaN(this.RefreshInterval))
         this.RefreshInterval = 30;
      this.DisplayMode = parseInt(System.Gadget.Settings.read("DisplayMode"));
      if(isNaN(this.DisplayMode))
         this.DisplayMode = 0;
      this.AutoScrollEnabled = System.Gadget.Settings.read("AutoScrollEnabled");
      this.AutoScrollInterval = parseInt(System.Gadget.Settings.read("AutoScrollInterval"));
      if(isNaN(this.AutoScrollInterval))
         this.AutoScrollInterval = 0;
      this.ProjectList = this.DeserializeProjectList(System.Gadget.Settings.read("ProjectList"));
   },
   AddProjectSettings: function(project)
   {
	   this.ProjectList[this.ProjectList.length]=project;
   },
   RemoveProjectSettings: function(project)
   {
	   for(var i in this.ProjectList)
	   {
	      if(this.ProjectList[i].serverUrl==project.serverUrl && this.ProjectList[i].projectName==project.projectName)
	      {
	         this.ProjectList.splice(i,1);
	         return;
	      }
	   }
   },
   SwapProjectSettings: function(i1,i2)
   {
      var tmp = this.ProjectList[i1];
      this.ProjectList[i1] = this.ProjectList[i2];
      this.ProjectList[i2] = tmp;
   },
   SerializeProjectList: function()
   {
		var projectListStr="";
		for(var i in this.ProjectList)
		{
			projectListStr+=this.ProjectList[i].serverUrl+"|"+this.ProjectList[i].projectName+";";
		}
		return projectListStr.substring(0,projectListStr.length-1);
   },
   DeserializeProjectList: function(listStr)
   {
		var projectList = new Array();
		if(listStr.length>0)
		{
			var projectListStr=listStr.split(";");
			for(var i in projectListStr)
			{
				var objParts = projectListStr[i].split("|");
				projectList[i] = new ProjectSettings(objParts[0],objParts[1]);
			}
		}
		return projectList;
   },
   Validate: function()
   {
      if(isNaN(parseInt(this.RefreshInterval)))
         return false;
      if(isNaN(parseInt(this.DisplayMode)))
         return false;
      if(isNaN(parseInt(this.AutoScrollInterval)))
         return false;
      return true;
   },
   GetServerList: function()
   {
      var serverList = new Array();
      var j=0;
      for(var i in this.ProjectList)
      {
         if(!ArrayContains(serverList,this.ProjectList[i].serverUrl))
            serverList.push(this.ProjectList[i].serverUrl)
      }
      return serverList;
   }
}

function ArrayContains(array,element)
{
      for(var i in array)
      {
         if(array[i]==element)
            return true;
      }
      return false;
}

var ProjectSettings = function(serverUrl, projectName)
{
   this.serverUrl = serverUrl;
   this.projectName = projectName;
   this.serverName = serverUrl.substring(0,serverUrl.length);
}

